package com.example.demo.controller;


import com.example.demo.entity.ClassQueryParam;
import com.example.demo.entity.Clazz;
import com.example.demo.entity.PageBean;
import com.example.demo.entity.Result;
import com.example.demo.service.Impl.ClassServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class ClassController {
    @Autowired
    private ClassServiceImpl classService;


    /**
     * 查询班级，分页
     * @param param
     * @return
     */
    @GetMapping("/clazzs")
    public Result page(ClassQueryParam param) {
        log.info(" ClassQueryParam 请求参数：{}", param);
        PageBean pageBean = classService.page(param);
        return Result.success(pageBean);
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/clazzs/list")
    public Result getAllClazz(){
        log.info(" 查询所有班级");
        return Result.success(classService.getAllClazz());
    }


    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @PostMapping("/clazzs")
    public Result save(@RequestBody Clazz clazz) {
        log.info(" 班级 请求参数：{}", clazz);
        classService.save(clazz);
        return Result.success();
    }

    /**
     * 根据指定id 查询班级信息 返回 Clazz 对象数据
     */
    @GetMapping("/clazzs/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info(" 根据id查询班级信息，id：{}", id);
        return Result.success(classService.findById(id));
    }

    /**
     * 修改班级信息
     * @param clazz
     * @return
     */
    @PutMapping("/clazzs")
    public Result update(@RequestBody Clazz clazz) {
        log.info(" 修改班级信息：{}", clazz);
        classService.update(clazz);
        return Result.success();
    }

    @DeleteMapping("/clazzs/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除班级：{}",id);
        classService.delete(id);
        return Result.success();
    }
}
